//
//  OptionViewController.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-22.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "OptionViewController.h"
#import "AudioManager.h"
#import "GameDataManager.h"
#import "FontManager.h"
#import "EasyGlyph.h"

@implementation OptionViewController

@synthesize starBackgroundA;
@synthesize starBackgroundB;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView* bgmImageView = (UIImageView*)[self.view viewWithTag:101];
    UIImageView* sfxImageView = (UIImageView*)[self.view viewWithTag:102];
    UIImageView* difficultView = (UIImageView*)[self.view viewWithTag:103];
    
    if (![[GameDataManager sharedInstance] bgm])
        bgmImageView.highlighted = YES;
    
    if (![[GameDataManager sharedInstance] sfx])
        sfxImageView.highlighted = YES;
    
    difficultView.image = [self GetDifficultImage];
    
    
    starBackgroundA.frame = CGRectMake(0, 0, 480, 320);
    starBackgroundB.frame = CGRectMake(0, 320, 480, 320);
    [UIView animateWithDuration: 10.0  delay: 0.0
                        options: UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat
                     animations: ^(){
                         starBackgroundA.frame = CGRectMake(0, -320, 320, 480);
                         starBackgroundB.frame = CGRectMake(0, 0, 480, 320);
                     }
                     completion: nil
     ];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return YES;
    
    return NO;
}

#pragma mark - touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    
    if (touch.view.tag == 100)
    {
        [[GameDataManager sharedInstance] saveData];
        [[AudioManager sharedInstance] PlaySFX:@"BackSFX"];
        [self performSegueWithIdentifier:@"SegueToMainViewController" sender:self];
    }
    else if (touch.view.tag == 101 || touch.view.tag == 102)
    {
        UIImageView* pImageView = (UIImageView*)touch.view;
        pImageView.highlighted = !pImageView.highlighted;
        
        if (pImageView.tag == 101)
            [[AudioManager sharedInstance] SwithBGMState];
        else
            [[AudioManager sharedInstance] SwithSFXState];
        
        [[AudioManager sharedInstance] PlaySFX:@"SelectSFX"];
    }
    else if (touch.view.tag == 104)
    {
        [[GameDataManager sharedInstance] switchDiffculty];
        
        UIImageView* pImageView = (UIImageView*)[self.view viewWithTag:103];
        pImageView.image = [self GetDifficultImage];
        
        [[AudioManager sharedInstance] PlaySFX:@"SelectSFX"];
    }
}

- (UIImage*)GetDifficultImage
{
    int difficulty = [[GameDataManager sharedInstance] difficulty];
    
    NSString* text = nil;
    
    switch (difficulty)
    {
        case 1:
            text = @"EASY";
            break;
        case 2:
            text = @"HARD";
            break;
        case 3:
            text = @"GOD";
            break;
    }
    
    return [[[FontManager sharedInstance] fontWithSize12] writeTextToTexture:text];
}


@end
